<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/3/24
 * Time: 11:00
 */

namespace App\Rep;


use App\Exceptions\ApiException;
use App\Models\FinishProduct;
use App\Models\Order;
use App\Models\SemiFinishProduct;
use Illuminate\Support\Facades\DB;

class SemiFinishProductRep
{

    /**
     * 获取半成品列表
     * @param $params
     */
    public function getSemiFinishProductListRep($params){
        //根据仓库分组
        $list = SemiFinishProduct::Search($params)
            ->where('pid',2)
            ->with(['warehouse'=>function($q){
                $q->select('id','warehouse_name');
            }])
            ->select('warehouse_id',DB::raw('count(*) as count'))
            ->groupBy('warehouse_id')
            ->paginate($params['per_page']??10);
        foreach($list as $k=>$v){
            //根据货架分组
            $goods_shelf_list = SemiFinishProduct::where('warehouse_id',$v->warehouse_id)
                ->where('is_del',2)
                ->with(['goods_shelf'=>function($q){
                    $q->select('id','goods_shelf_name');
                }])
                ->select('goods_shelf_id','warehouse_id',DB::raw('count(*) as count'))
                ->groupBy('goods_shelf_id')
                ->get();
            foreach($goods_shelf_list as $kk=>$vv){
                //根据型号分组
                $model_number_list = SemiFinishProduct::where('warehouse_id',$v->warehouse_id)
                    ->where('is_del',2)
                    ->where('goods_shelf_id',$vv->goods_shelf_id)
                    ->with(['model_number'=>function($q){
                        $q->select('id','model_number');
                    }])
                    ->select('model_number_id','goods_shelf_id','warehouse_id',DB::raw('count(*) as count'))
                    ->groupBy('model_number_id')
                    ->get();

                foreach($model_number_list as $kkk=>$vvv){
                    //根据规格分组
                    $specification_list = SemiFinishProduct::where('warehouse_id',$v->warehouse_id)
                        ->where('is_del',2)
                        ->where('goods_shelf_id',$vv->goods_shelf_id)
                        ->where('model_number_id',$vvv->model_number_id)
                        ->with(['specification'=>function($q){
                            $q->select('id','specification');
                        }])
                        ->select('specification_id','model_number_id','goods_shelf_id','warehouse_id',DB::raw('count(*) as count'))
                        ->groupBy('specification_id')
                        ->get();
                    $model_number_list[$kkk]->specification_list = $specification_list;
                }
                $goods_shelf_list[$kk]->model_number_list = $model_number_list;
            }
            $list[$k]->goods_shelf_list = $goods_shelf_list;
        }
        return $list;
    }

    public function getSemiFinishProductInfoRep($params){
        if($params['page_name'] == 'product'){
            $list = SemiFinishProduct::Search($params)
                ->with(['product'=>function($q){
                    $q->select('id','product_name');
                }])
                ->select('product_id','modelid',DB::raw('count(*) as count'))
                ->groupBy('product_id')
                ->paginate($params['per_page']??10);
        }elseif($params['page_name'] == 'model_number'){
            $list = SemiFinishProduct::Search($params)
                ->with(['model_number'=>function($q){
                    $q->select('id','model_number');
                }])
                ->select('model_number_id','modelid',DB::raw('count(*) as count'))
                ->groupBy('model_number_id')
                ->paginate($params['per_page']??10);
        }elseif($params['page_name'] == 'specification'){
            $list = SemiFinishProduct::Search($params)
                ->with(['specification'=>function($q){
                    $q->select('id','specification');
                }])
                ->select('specification_id','modelid',DB::raw('count(*) as count'))
                ->groupBy('specification_id')
                ->paginate($params['per_page']??10);
        } elseif($params['page_name'] == 'color'){
            $list = SemiFinishProduct::Search($params)
                ->with(['color'=>function($q){
                    $q->select('id','color');
                }])
                ->select('color_id','modelid',DB::raw('count(*) as count'))
                ->groupBy('color_id')
                ->paginate($params['per_page']??10);
        }

        return $list;
    }

    /**
     * 获取半成品详情列表
     * @param $params
     * @return void
     */
    public function getSemiFinishProductInfoListRep($params){
        $list = SemiFinishProduct::Search($params)
            ->where('pid',2)
            ->with(['warehouse'=>function($q){
                $q->select('id','warehouse_name');
            },'goods_shelf','model_number','specification','product','order'])
            ->paginate($params['per_page']??10);
        return $list;
    }

    /**
     * 加入半成品库
     */
    public function addSemiFinishProductRep($order_id,$order_second_id,$pid,$product_id,$model_number_id,$specification_id,$color_id,$num,$modelid,$is_del,$warehouse_id,$goods_shelf_id,$remark){
        for($i=0;$i<$num;$i++){
             SemiFinishProduct::insert(
                [
                    'order_id'=>$order_id,
                    'order_second_id'=>$order_second_id,
                    'product_id'=>$product_id,
                    'model_number_id'=>$model_number_id,
                    'specification_id'=>$specification_id,
                    'color_id'=>$color_id,
                    'modelid'=>$modelid,
                    'pid'=>$pid,
                    'finish_time'=>date("Y-m-d H:i:s"),
                    'is_del'=>$is_del,
                    'warehouse_id'=>$warehouse_id,
                    'goods_shelf_id'=>$goods_shelf_id,
                    'created_at'=>date('Y-m-d H:i:s'),
                    'remark'=>$remark
                ]
            );
        }
    }

    //获取库存数
    public function getInventoryCountRep($product_id,$model_number_id,$specification_id,$color_id,$warehouse_id,$goods_shelf_id){
        $model = SemiFinishProduct::where('is_del',2);
        //产品
        if(!empty($product_id)){
            $model->where('product_id',$product_id);
        }

        //型号
        if(!empty($model_number_id)){
            $model->where('model_number_id',$model_number_id);
        }

        //规格
        if(!empty($specification_id)){
            $model->where('specification_id',$specification_id);
        }

        //颜色
        if(!empty($color_id)){
            $model->where('color_id',$color_id);
        }

        if(!empty($warehouse_id)){
            $model->where('warehouse_id',$warehouse_id);
        }
        if(!empty($goods_shelf_id)){
            $model->where('goods_shelf_id',$goods_shelf_id);
        }
        $count = $model->count();
        return $count;
    }

    //从半成品备货库中获取备货ID
    public function getInventoryIdRep($product_id,$model_number_id,$specification_id,$warehouse_id,$goods_shelf_id){
        $model = SemiFinishProduct::where('is_del',2);
        //产品
        if(!empty($product_id)){
            $model->where('product_id',$product_id);
        }

        //型号
        if(!empty($model_number_id)){
            $model->where('model_number_id',$model_number_id);
        }

        //规格
        if(!empty($specification_id)){
            $model->where('specification_id',$specification_id);
        }
//        else{
//            $specification_id = 0;
//            $model->where('specification_id',$specification_id);
//        }

        if(!empty($warehouse_id)){
            $model->where('warehouse_id',$warehouse_id);
        }else{
            $warehouse_id = 0;
            $model->where('warehouse_id',$warehouse_id);
        }
        if(!empty($goods_shelf_id)){
            $model->where('goods_shelf_id',$goods_shelf_id);
        }
        $data = $model->pluck('id')->toArray();
        return $data;
    }



    /**
     * 半成品加入成品库
     * @param $params
     */
//    public function addFinishProductRep($params){
//        //p($params);
//        DB::beginTransaction();
//        try{
//            foreach ($params['ids'] as $k=>$v){
//                $semi = SemiFinishProduct::where('id',$v)->with('order')->first();
//                $semi->is_del = 1;
//                $semi->save();
//                $finishProductRep = new FinishProductRep();
//                $finishProductRep->addFinishProductRep(
//                    $semi->order_id,
//                    $semi->id,$params['num'],
//                    $params['warehouse_id'],
//                    $params['goods_shelf_id'],
//                    $semi->order->model_number_id,
//                    $semi->order->specification_id,
//                    $semi->order->color_id,
//                    $semi->order->direction,
//                    $semi->order->customer_id,
//                    $semi->order->is_stock_up,
//                    $params['image_arr']??[]
//                );
//                $order = Order::where('id',$semi->order_id)->first();
//                $order->production_status = 3;
//                $order->save();
//            }
//
//            DB::commit();
//            return ['code'=>0,'data'=>'','msg'=>'success'];
//
//        }catch(\Exception $exception){
//            DB::rollBack();
//            return ['code'=>2001,'data'=>'','msg'=>$exception->getMessage()];
//        }
//    }

    /**
     * 补录订单直接添加到成品库
     */
//    public function replenishmentAddFinishProductRep($order_id,$product_id,$num,$warehouse_id,$goods_shelf_id){
//        $finishProductRep = new FinishProductRep();
//        return $finishProductRep->addFinishProductRep($order_id,$product_id,$num,$warehouse_id,$goods_shelf_id,$order->model_number_id,$order->specification_id,$order->color_id,$order->direction,$order->customer_id,$order->is_stock_up);
//    }

    /**
     * 删除半成品
     * @param $params
     * @return void
     */
    public function delSemiFinishProductRep($params){
        $semi = SemiFinishProduct::whereIn('id',$params['ids'])->get();
        foreach($semi as $k=>$v){
            if(!empty($v->production_order_child_id)){
                throw new ApiException(20001,'订单的库存已被使用，无法删除');
            }
            $v->is_del = 2;
            $v->remark = $v->remark.';删除半成品库存';
            $v->save();
        }
    }
}
